﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Collections.ObjectModel;
using DocumentManagement.Models;

namespace DocumentManagement.ViewsModels
{
  public  class ShowItemDocumentOutViewsModel : ViewsModelBase
    {
      private QSHCEntities1 _entityModels;
        public QSHCEntities1 EntityModels
        {
            get { return _entityModels; }
            set
            {
                if (_entityModels != value)
                {
                    _entityModels = value;
                    OnPropertyChanged("EntityModels");
                }
            }
        }
        private ICollectionView _mySendICV;
        public ICollectionView MySendICV
        {
            get { return _mySendICV; }
            set
            {
                if (_mySendICV != value)
                {
                    _mySendICV = value;
                    OnPropertyChanged("MySendICV");
                }
            }
        }

        private CreateSend _mySelectedEditItemSend;
        public CreateSend MySelectedEditItemSend
        {
            get { return _mySelectedEditItemSend; }
            set
            {
                if (_mySelectedEditItemSend != value)
                {
                    _mySelectedEditItemSend = value;
                    OnPropertyChanged("MySelectedEditItemSend");
                }
            }
        }


  
      public ShowItemDocumentOutViewsModel ()
	{

            if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                EntityModels = new QSHCEntities1();

                MySendICV = CollectionViewSource.GetDefaultView(EntityModels.CreateSends.Where(p => p.Active == true).ToList());
              
            }
	}
      public void DeleteFromDatabase()
      {

          var dr = MessageBox.Show("ต้องการลบข้อมูลหรือไม่", "ยืนยันการลบข้อมูล", MessageBoxButton.OKCancel, MessageBoxImage.Warning, MessageBoxResult.OK);
          if (dr == MessageBoxResult.OK)
          {
              var deleteCreateSend = (EntityModels.CreateSends.Where(p => p.CreateSendID == MySelectedEditItemSend.CreateSendID).Single());
              deleteCreateSend.Active = false;
              //var deleteCreateSend = (EntityModels.CreateSends.Where(p => p.CreateSendCode == MySelectedEditItemSend.CreateSendCode).Single());
              //deleteCreateSend.Active = false;


              foreach (var item in EntityModels.ReceiveAttachedFiles)
              {
                  var a = item.ReceiveID;
                  if (a == MySelectedEditItemSend.CreateSendID)
                  {
                      item.Active = false;

                  }
              }
              EntityModels.SaveChanges();

              MySendICV = CollectionViewSource.GetDefaultView(EntityModels.CreateSends.Where(p => p.Active == true).ToList());
              MySendICV.Refresh();


          }
      }
      public void RefreshData()
      {
          MySendICV = CollectionViewSource.GetDefaultView(EntityModels.CreateSends.Where(p => p.Active == true).ToList());
      }

        public void Filter(string txtFilter, DateTime? startdate, DateTime? enddate)
        {
            MySendICV = CollectionViewSource.GetDefaultView(EntityModels.CreateSends.Where(p => p.Active == true).ToList());


            MySendICV.Filter += (o) =>
            {
                CreateSend p = (o as CreateSend);

                if (p == null)

                    return false;

                if (txtFilter != "" && startdate == null && enddate == null)
                {
                    if (p.CreateSendCode.Contains(txtFilter) || p.Title.Contains(txtFilter))
                        return true;
                    else
                        return false;
                }

                if (startdate != null && enddate != null && txtFilter == "")
                {
                    if (p.CreateSendDate.Date >= startdate.Value.Date && p.CreateSendDate.Date <= enddate.Value.Date)
                        return true;
                    else
                        return false;
                }

                if (startdate != null && enddate == null && txtFilter == "")
                {
                    if (p.CreateSendDate.Date >= startdate.Value.Date)
                        return true;
                    else
                        return false;
                }

                if (enddate != null && startdate == null && txtFilter == "")
                {
                    if (p.CreateSendDate.Date <= enddate.Value.Date)
                        return true;
                    else
                        return false;
                }

                if (txtFilter != "" && startdate != null && enddate == null)
                {
                    if ((p.CreateSendCode.Contains(txtFilter) || p.Title.Contains(txtFilter)) && p.CreateSendDate.Date >= startdate.Value.Date)
                        return true;
                    else
                        return false;
                }

                if (txtFilter != "" && startdate == null && enddate != null)
                {
                    if ((p.CreateSendCode.Contains(txtFilter) || p.Title.Contains(txtFilter)) && p.CreateSendDate.Date <= enddate.Value.Date)
                        return true;
                    else
                        return false;
                }

                if (startdate != null && enddate != null && txtFilter != "")
                {

                    if ((p.CreateSendCode.Contains(txtFilter) || p.Title.Contains(txtFilter)) && (p.CreateSendDate.Date >= startdate.Value.Date && p.CreateSendDate.Date <= enddate.Value.Date))
                        return true;
                    else
                        return false;

                }

                return true;
               
            };
            
        }

       


      
   
    }
}
